package com.example.fill;

import android.content.Context;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class StrLayout extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder holder;
    private Canvas canvas;
    private boolean isDrawing = false;
    private Thread thread;
    private RunThread runThread;

    private static List<RandomColumn> data = new ArrayList<>();

    static {
        int tick = 0;
        for (int i = 0; i < 300; i++) {
            RandomColumn randomColumn = new RandomColumn();
            tick += -randomColumn.getTick() * 5 / 6;
            randomColumn.setLocation(tick);
            data.add(randomColumn);
        }
    }

    public StrLayout(Context context) {
        super(context);
        init();
    }

    public StrLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StrLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public StrLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init() {
        holder = getHolder();
        holder.addCallback(this);
        //holder.setFormat(PixelFormat.RGBA_8888);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);

    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
//        isDrawing = true;
//        thread = new Thread(this);
//        thread.start();
//        runThread = new RunThread();
//        runThread.start();
        int colorBg = ContextCompat.getColor(getContext(), R.color.md_theme_dark_onPrimary);
        int[] colors = {ColorUtils.toAlpha(colorBg, 0.01f), ColorUtils.toAlpha(colorBg, 0.25f), ColorUtils.toAlpha(colorBg, 0.01f)};
        float[] positions = {0, 0.5f, 1};
        LinearGradient linearGradient = new LinearGradient(0, 0, 0, getHeight(), colors, positions, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
        startDraw();
    }

    int width;
    int height;

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        this.width = width;
        this.height = height;

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        while (thread.isAlive()) {
            isDrawing = false;
        }
    }

    @Override
    public void run() {
//        while (isDrawing) {
//            startDraw();
//        }
        startDraw();
    }

    Paint paint = new Paint();

    private void startDraw() {
        canvas = holder.lockCanvas();
        if (canvas != null) {
            canvas.drawColor(Color.WHITE);
//            for (RandomColumn datum : data) {
//                canvas.drawRect(datum.getLeft(), datum.getLocation(), datum.getRight(), datum.getLocation()
//                        + 200, paint);
//                datum.setLocation(datum.getLocation() + move);
//            }
            canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    private int move = 0;

    class RunThread extends Thread {
        @Override
        public void run() {
            move += 8;
        }
    }
}
